﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using DevExpress.Utils.Drawing;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Ribbon.Gallery;
using DevExpress.XtraBars.Ribbon.ViewInfo;
using XCI.Helper;

namespace XCI.Windows.Helper
{
    /// <summary>
    /// Gallery控件辅助操作
    /// </summary>
    public class GalleryHelper
    {
        /// <summary>
        /// 标记项列表
        /// </summary>
        private List<GalleryItem> markedItems;
        /// <summary>
        /// 
        /// </summary>
        protected List<GalleryItem> MarkedItems
        {
            get { return markedItems ?? (markedItems = new List<GalleryItem>()); }
        }

        /// <summary>
        /// 当前Gallery控件
        /// </summary>
        public GalleryControl Gallery { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gallery"></param>
        /// <param name="imageSize"></param>
        public GalleryHelper(GalleryControl gallery, Size imageSize)
        {
            Gallery = gallery;
            Gallery.Gallery.ImageSize = imageSize;
            Gallery.Gallery.ItemCheckedChanged += Gallery_ItemCheckedChanged;
            Gallery.Gallery.CustomDrawItemImage += Gallery_CustomDrawItemImage;
        }

        #region 方法

        private RectangleF InflateClip(GraphicsCache cache)
        {
            RectangleF clipBounds = cache.Graphics.ClipBounds;
            RectangleF rect = cache.Graphics.ClipBounds;
            rect.Inflate(50, 50);
            cache.Graphics.SetClip(rect);
            return clipBounds;
        }

        private void DrawMarkedIcon(GraphicsCache cache, Rectangle bounds)
        {
            RectangleF oldClipBounds = InflateClip(cache);
            cache.Graphics.DrawImage(global::XCI.Windows.Properties.Resources.Mark32, 
                new Rectangle(bounds.Right - 26, bounds.Bottom - 26, 32, 32));
            cache.Graphics.SetClip(oldClipBounds);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        public void MarkItems(IEnumerable<GalleryItem> items)
        {
            foreach (GalleryItem item in items)
            {
                if (!MarkedItems.Contains(item))
                {
                    MarkedItems.Add(item);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        public void UnmarkItems(IEnumerable<GalleryItem> items)
        {
            foreach (GalleryItem item in items)
            {
                MarkedItems.Remove(item);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void CleanMarks()
        {
            MarkedItems.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string[] GetSelectedFileNames()
        {
            List<string> list = new List<string>();
            foreach (GalleryItem item in MarkedItems)
            {
                list.Add(item.Tag.ToString());
            }
            return list.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetSelectedFileName()
        {
            if (MarkedItems.Count>0)
            {
                return MarkedItems[0].Tag.ToString();
            }
            return string.Empty;
        }

        //private void ClearGalleryItemsCheckState(GalleryControl gallery)
        //{
        //    foreach (GalleryItemGroup group in gallery.Gallery.Groups)
        //    {
        //        foreach (GalleryItem item in group.Items)
        //        {
        //            item.Checked = false;
        //        }
        //    }
        //}
        
        /// <summary>
        /// 添加图片
        /// </summary>
        public void AddImage(string[] files, string selectedName)
        {
            if (Gallery.Gallery.Groups.Count == 0)
            {
                GalleryItemGroup group1 = new GalleryItemGroup();
                Gallery.Gallery.Groups.Add(group1);
            }
            foreach (string file in files)
            {
                var group = Gallery.Gallery.Groups[0];
                Image image = ImageHelper.LoadImage(file);
                var item = new GalleryItem(image, "", "");
                string fileName = Path.GetFileName(file);
                item.Tag = fileName;
                group.Items.Add(item);
                if (!string.IsNullOrEmpty(selectedName) && selectedName.Equals(fileName))
                {
                    MarkedItems.Add(item);
                    item.Checked = true;
                }
            }
        }


        #endregion

        #region 事件
        
        private void Gallery_ItemCheckedChanged(object sender, GalleryItemEventArgs e)
        {
            GalleryControl gallery = ((GalleryControlGallery)sender).GalleryControl;
            CleanMarks();
            List<GalleryItem> items = gallery.Gallery.GetCheckedItems();
            MarkItems(items);
            gallery.Refresh();
            //if (items != null
            //    && items.Count > 0
            //    && gallery.Tag != null)
            //{
            //    //CheckedChangedAction(CurrentGalleryControl.Tag.ToString(), items[0].Tag.ToString());
            //}
        }

        private void Gallery_CustomDrawItemImage(object sender, GalleryItemCustomDrawEventArgs e)
        {
            if (!MarkedItems.Contains(e.Item)) return;
            e.Cache.Graphics.DrawImage(e.Item.Image, ((GalleryItemViewInfo)e.ItemInfo).ImageContentBounds);
            DrawMarkedIcon(e.Cache, ((GalleryItemViewInfo)e.ItemInfo).ImageContentBounds);
            e.Handled = true;
        }

        #endregion
    }
}
